package com.bonc.gen.controller;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import com.bonc.gen.mapper.GenMapper;
import com.bonc.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping
public class Gen {

    @Autowired
    private GenMapper genMapper;

    @GetMapping("/gen")
    public ResultVo<List<String>> gen(){
        FastAutoGenerator.create("jdbc:mysql://localhost:3306/oauth2?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=true", "root", "123456")
                .globalConfig(builder -> {
                    builder.author("bonc") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir("E:\\springboot-mybatisplus\\src\\main\\java\\"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.bonc") // 设置父包名
                            .moduleName("insight") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "E:\\springboot-mybatisplus\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude(genMapper.getTableName()).entityBuilder().idType(IdType.ASSIGN_UUID);// 设置需要生成的表名
//                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                })
                .templateEngine(new VelocityTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
        return ResultVo.getSuccess();
    }
}
